package it.polimi.travelDream.ejb.servlet;


import it.polimi.travelDream.ejb.Cthulhu.Cthulhu;
import it.polimi.travelDream.ejb.interfaces.UserMgrRemote;
import it.polimi.travelDream.entities.User;

import java.io.IOException;
import java.util.Map;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//copiato
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
        
        private static final long serialVersionUID = -7419935089593893820L;
        
        private User u = null;
        @EJB
        private UserMgrRemote umr;
       
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
    }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                this.doPost(request, response);
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        	Map<String, String[]> mappa = request.getParameterMap();
        	String username=mappa.get("username")[0];
        	String password=mappa.get("password")[0];
        	
        	System.out.println(username+password);
        	u=umr.validate_login(username, password);
        	if (u!=null){
        		request.getSession().setAttribute("utente", u);
        		System.out.println("ciao"+u.getId());
        		request.getRequestDispatcher("/UHome.jsp").forward(request, response); 
        	}
        	else {
        		
        		 request.setAttribute("message","Wrong user or password");
					request.getRequestDispatcher("/login.jsp").forward(request, response); 
        		
        	}
        	
        	
        	
        }

        
        protected void addToRequestProfile(HttpServletRequest request) {
        	              
        }
}